package com.example.springboot.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.springboot.common.Constants;
import com.example.springboot.common.SnowflakeManager;
import com.example.springboot.entity.Order;
import com.example.springboot.exception.ServiceException;
import com.example.springboot.mapper.OrderMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class OrderService extends ServiceImpl<OrderMapper,Order> {
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private SnowflakeManager snowflakeManager;
    @Autowired
    private BookService bookService;
    public Order create(Order order) throws Exception {
        order.setOid(snowflakeManager.nextValue());
//        Order one=getOrderInfo(order);
//        if(one==null)
//        {
            save(order);
//        }else{
//            throw new ServiceException(Constants.CODE_600,"订单已存在");
//        }
//        减少库存
        bookService.releaseBook(order.getISBN(),order.getQuantity());
        return order;
    }
//    private Order getOrderInfo(Order order){
//        QueryWrapper<Order> queryWrapper=new QueryWrapper<>();
//        queryWrapper.eq("oid",order.getOid());
//        Order one;
//        try{
//            one=getOne(queryWrapper);
//        }catch (Exception e){
//            throw new ServiceException(Constants.CODE_500,"系统错误");
//        }
//        return one;
//    }
}
